//
//  TTGeneralTableViewCell.m
//  TaoTuo
//
//  Created by admin on 2019/11/8.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTGeneralTableViewCell.h"

@interface TTGeneralTableViewCell ()

@property (nonatomic, strong) UIImageView *pictureView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UILabel *detailLabel;
@property (nonatomic, strong) UIButton *rightBtn;
@property (nonatomic, strong) UISwitch *mySwitch;

@end

@implementation TTGeneralTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

// 点击了右侧按钮
- (void)rightBtnClick:(UIButton *)sender
{
    LLGeneralButtonCellModel *model = (LLGeneralButtonCellModel *)self.model;
//    sender.selected = !sender.selected;
//    model.isSelect = sender.selected;
    if (model.clickBlock) {
        model.clickBlock(model, sender);
    }
}

// 点击了switch
- (void)mySwitchChangeValue:(UISwitch *)sender
{
    NSLog(@"== > %d", sender.on);
    LLGeneralSwitchCellModel *model = (LLGeneralSwitchCellModel *)self.model;
//    model.isOn = sender.on;
    if (model.switchBlock) {
        model.switchBlock(model, self.mySwitch);
    }
}

- (void)setModel:(LLGeneralCellModel *)model
{
    _model = model;
    // 图片
    if (model.imageName) {
        self.pictureView.image = [UIImage imageNamed:model.imageName];
    } else {
        self.pictureView.image = nil;
    }
    // 标题
    self.titleLabel.text = model.title;
    //内容
    if (model.contentAttributedString) {
        self.contentLabel.attributedText = model.contentAttributedString;
    }
    // 右侧内容
    if (model.detailAttributedString) {
        self.detailLabel.attributedText = model.detailAttributedString;
    }
    
    // 右侧视图
    if ([model isKindOfClass:[LLGeneralSwitchCellModel class]]) {
        // 开关按钮
        [self setupSwitchCell];
    } else if ([model isKindOfClass:[LLGeneralArrowCellModel class]]) {
        // 箭头
        [self setupArrowCell];
    } else if ([model isKindOfClass:[LLGeneralButtonCellModel class]]) {
        // 按钮
        [self setupBtnCell];
    } else {
        // 普通的
        [self setupGeneralCell];
    }
    
    
    [self setNeedsLayout];
    
}

- (void)setupGeneralCell
{
    self.rightBtn.hidden = YES;
    self.mySwitch.hidden = YES;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

- (void)setupArrowCell
{
    self.rightBtn.hidden = NO;
    self.mySwitch.hidden = YES;
    self.rightBtn.enabled = NO;
    self.selectionStyle = UITableViewCellSelectionStyleDefault;
    
    LLGeneralArrowCellModel *model = (LLGeneralArrowCellModel *)self.model;
    NSString *name = [NSString stringWithFormat:@"cellarrow_%lu", (unsigned long)model.arrowDirection];
    [self.rightBtn setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
    
    
    
}

- (void)setupBtnCell
{
    self.rightBtn.hidden = NO;
    self.mySwitch.hidden = YES;
    self.rightBtn.enabled = YES;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    LLGeneralButtonCellModel *model = (LLGeneralButtonCellModel *)self.model;
    [self.rightBtn setImage:[UIImage imageNamed:model.rightNormalImageName] forState:UIControlStateNormal];
    [self.rightBtn setImage:[UIImage imageNamed:model.rightSelectedImageName] forState:UIControlStateSelected];
    self.rightBtn.selected = model.isSelect;
    model.button = self.rightBtn;
    
}

- (void)setupSwitchCell
{
    self.rightBtn.hidden = YES;
    self.mySwitch.hidden = NO;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    LLGeneralSwitchCellModel *model = (LLGeneralSwitchCellModel *)self.model;
    self.mySwitch.on = model.isOn;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    UIImage *image = self.pictureView.image;
    
    [self.pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.centerY.equalTo(self.contentView);
        make.width.mas_equalTo(image.size.width);
        make.height.mas_equalTo(image.size.height);
    }];
    
    CGFloat offsetLeft = image ? 8 : 0;
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.pictureView.mas_right).mas_offset(offsetLeft);
        make.centerY.equalTo(self.contentView);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_right).mas_offset(5);
        make.centerY.equalTo(self.contentView);
        make.width.mas_lessThanOrEqualTo(200 * kScale);
    }];
    
    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.equalTo(self.contentView);
        make.width.mas_equalTo(30);
    }];
    
    [self.mySwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).mas_offset(-5);
        make.centerY.equalTo(self.contentView);
    }];
    
    CGFloat offsetRight;
    MASViewAttribute *leftAttr;
    if ([self.model isKindOfClass:[LLGeneralSwitchCellModel class]]) {
        leftAttr = self.mySwitch.mas_left;
        offsetRight = 2;
    } else if ([self.model isKindOfClass:[LLGeneralArrowCellModel class]] || [self.model isKindOfClass:[LLGeneralButtonCellModel class]]) {
        leftAttr = self.rightBtn.mas_left;
        offsetRight = 0;
    } else {
        leftAttr = self.contentView.mas_right;
        offsetRight = 10;
    }
    
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(leftAttr).mas_offset(-offsetRight);
        make.centerY.equalTo(self.contentView);
    }];
    
}


- (UIImageView *)pictureView
{
    if (!_pictureView) {
        _pictureView = [[UIImageView alloc] init];
        _pictureView.contentMode = UIViewContentModeCenter;
        [self.contentView addSubview:_pictureView];
    }
    return _pictureView;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:15];
        [self.contentView addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (UILabel *)contentLabel
{
    if (!_contentLabel) {
        _contentLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:15];
        [self.contentView addSubview:_contentLabel];
    }
    return _contentLabel;
}

- (UILabel *)detailLabel
{
    if (!_detailLabel) {
        _detailLabel = [UILabel labelWithTitle:@"" color:UIColorHex(999999) fontSize:12];
        [self.contentView addSubview:_detailLabel];
    }
    return _detailLabel;
}

- (UIButton *)rightBtn
{
    if (!_rightBtn) {
        _rightBtn = [UIButton createButtonWithImage:[UIImage new] target:self action:@selector(rightBtnClick:)];
        [self.contentView addSubview:_rightBtn];
    }
    return _rightBtn;
}

- (UISwitch *)mySwitch
{
    if (!_mySwitch) {
        _mySwitch = [[UISwitch alloc] init];
        _mySwitch.onTintColor = kMainColor;
        _mySwitch.transform = CGAffineTransformMakeScale(0.75, 0.75);
        [_mySwitch addTarget:self action:@selector(mySwitchChangeValue:) forControlEvents:UIControlEventValueChanged];
        [self.contentView addSubview:_mySwitch];
    }
    return _mySwitch;
}

@end
